package edu.pitt.dbmi.odie.gapp.gwt.client.analysis;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

import edu.pitt.dbmi.odie.gapp.gwt.client.ODIE_TabbedNavigator;
import edu.pitt.dbmi.odie.gapp.gwt.client.dao.ODIE_DaoAnalysis;
import edu.pitt.dbmi.odie.gapp.gwt.client.util.ODIE_ClientUtils;
import edu.pitt.dbmi.odie.gapp.gwt.client.util.panel.ODIE_ButtonItemPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.util.panel.ODIE_IButtonPanel;

public class ODIE_AnalysesManagementPanel extends VLayout {

	/**
	 * Create a remote service proxy to talk to the server-side ODIE_ObaService
	 * service.
	 */
	private final ODIE_AnalysisServiceAsync analysisService = GWT
			.create(ODIE_AnalysisService.class);

	private final ListGrid analysesListGrid = new ListGrid();

	private IButton navigateButton = null;

	private IButton deleteButton = null;

	public ODIE_AnalysesManagementPanel() {
		setLayoutMargin(10) ;
		setMembersMargin(10) ;
		addMember(buildAnalysisGroup());
		addMember(buildActionPanel());
		draw();
	}

	private VLayout buildAnalysisGroup() {
		this.analysesListGrid.setAlternateRecordStyles(true) ;
		this.analysesListGrid.setCanAcceptDrop(false);
		this.analysesListGrid.setCanEdit(false);
		this.analysesListGrid.setCanSort(false);
		this.analysesListGrid.setCanSelectAll(false);
		this.analysesListGrid.setCanFreezeFields(false);
		this.analysesListGrid.setCanGroupBy(false);
		this.analysesListGrid.setCanReorderFields(false);
		this.analysesListGrid.setCanResizeFields(false);
		this.analysesListGrid.setAutoFetchData(true);
		this.analysesListGrid.setShowFilterEditor(false);
		this.analysesListGrid.setShowHeaderContextMenu(false);
		this.analysesListGrid.setAlign(Alignment.CENTER);
		this.analysesListGrid
				.addRecordDoubleClickHandler(buildAnalysisRecordDoubleClickListener());
		this.analysesListGrid.setDataSource(buildAnalysesDataSource());
		this.analysesListGrid.setSortField("title");
		this.analysesListGrid.setSortDirection(SortDirection.DESCENDING) ;
		
		this.analysesListGrid.addDataArrivedHandler(buildDataArrivedHandler());
		VLayout group = new VLayout();
		group.setWidth100();
		group.setHeight("80%");
		group.setLayoutMargin(10);
		group.setIsGroup(true);
		group.setGroupTitle("Analysis");
		group.addMember(this.analysesListGrid);
		return group;
	}



	private DataSource buildAnalysesDataSource() {

		DataSource dataSource = new DataSource();
		dataSource.setDataFormat(DSDataFormat.XML);
		String xPathExpression = "//analysis/analysisStatus[.='IDLING']/..";
		dataSource.setRecordXPath(xPathExpression);
		dataSource.setDataURL("odie_client/snapshot?category=analysis");

		dataSource.setShowPrompt(false);

		DataSourceTextField idField = new DataSourceTextField("id", "id");
		idField.setHidden(true);

		DataSourceTextField titleField = new DataSourceTextField("title",
				"Name");
		titleField.setCanFilter(false);
		titleField.setLength(60);

		DataSourceTextField modifiedField = new DataSourceTextField(
				"modifiedDate", "Completion Date");
		modifiedField.setCanFilter(false);
		modifiedField.setLength(25);

		DataSourceTextField numberOfDocumentsField = new DataSourceTextField(
				"unitsCompleted", "#Documents Coded");
		numberOfDocumentsField.setCanFilter(false);
		numberOfDocumentsField.setLength(15);
		
		DataSourceTextField isSelectedField = new DataSourceTextField(
				"isSelected", "isSelected");
		isSelectedField.setHidden(true) ;

		dataSource.setFields(idField, titleField, numberOfDocumentsField,
				modifiedField, isSelectedField);

		return dataSource;
	}

	public void refresh() {
		this.analysesListGrid.setDataSource(buildAnalysesDataSource());
		this.analysesListGrid.fetchData() ;
	}
	
	private DataArrivedHandler buildDataArrivedHandler() {
		return new DataArrivedHandler() {
			public void onDataArrived(DataArrivedEvent event) {
				ODIE_ClientUtils.selectBasedOnData(ODIE_AnalysesManagementPanel.this.analysesListGrid) ;
				ODIE_AnalysesManagementPanel.this.analysesListGrid.draw();
			}
		};
	}
	
	private void performNavigation(String odieId) {
		ODIE_DaoAnalysis analysisDAO = new ODIE_DaoAnalysis();
		analysisDAO.setOdieId(odieId);
		analysisService.select(analysisDAO,
				new AsyncCallback<ODIE_DaoAnalysis>() {
					public void onFailure(Throwable error) {
						;
					}
					public void onSuccess(ODIE_DaoAnalysis result) {
//						ODIE_TabbedNavigator.self.setCurrentAnalysis(result) ;
						ODIE_TabbedNavigator.self.selectTab("Navigator") ;
					}
				});
	}

	private void performDeletion(String odieId) {
		ODIE_DaoAnalysis analysisDAO = new ODIE_DaoAnalysis();
		analysisDAO.setOdieId(odieId);
		analysisService.delete(analysisDAO, new AsyncCallback<Void>() {
			public void onFailure(Throwable error) {
				;
			}

			public void onSuccess(Void result) {
				refresh();

			}
		});
	}

	private RecordDoubleClickHandler buildAnalysisRecordDoubleClickListener() {
		return new RecordDoubleClickHandler() {
			public void onRecordDoubleClick(RecordDoubleClickEvent event) {
				performNavigation(event.getRecord().getAttribute("id"));
			}
		};
	}
	
//	com.smartgwt.client.widgets.events.
	
	private Canvas buildActionPanel2() {
		
		final IButton navigateIButton = ODIE_IButtonPanel.buildIButton("Navigate") ;
		navigateIButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
				ListGridRecord selectedRecord = ODIE_AnalysesManagementPanel.this.analysesListGrid
						.getSelectedRecord();
				if (selectedRecord != null) {
					String odieAnalysisId = selectedRecord.getAttribute("id");
					performNavigation(odieAnalysisId);
				}
				else {
					;
				}
			}
		});
		
		final IButton deleteIButton = ODIE_IButtonPanel.buildIButton("Delete") ;
		deleteIButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
				ListGridRecord selectedRecord = ODIE_AnalysesManagementPanel.this.analysesListGrid
						.getSelectedRecord();
				if (selectedRecord != null) {
					String odieAnalysisId = selectedRecord.getAttribute("id");
					performDeletion(odieAnalysisId);
				}
				else {
					;
				}
			}
		});
		
		ArrayList<IButton> buttons = new ArrayList<IButton>() ;
		buttons.add(navigateIButton) ;
		buttons.add(deleteIButton) ;
		
		return new ODIE_IButtonPanel("Actions", buttons) ;

	}
	
	private Canvas buildActionPanel() {
		
		final ButtonItem navigateButtonItem = ODIE_ButtonItemPanel.buildButtonItem("Navigate") ;
		navigateButtonItem.setStartRow(true) ;
		navigateButtonItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ListGridRecord selectedRecord = ODIE_AnalysesManagementPanel.this.analysesListGrid
						.getSelectedRecord();
				if (selectedRecord != null) {
					String odieAnalysisId = selectedRecord.getAttribute("id");
					performNavigation(odieAnalysisId);
				}
				else {
					;
				}
				
			}
		});
		
		final ButtonItem deleteButtonItem = ODIE_ButtonItemPanel.buildButtonItem("Delete") ;
		deleteButtonItem.setEndRow(true) ;
		deleteButtonItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ListGridRecord selectedRecord = ODIE_AnalysesManagementPanel.this.analysesListGrid
						.getSelectedRecord();
				if (selectedRecord != null) {
					String odieAnalysisId = selectedRecord.getAttribute("id");
					performDeletion(odieAnalysisId);
				}
				else {
					;
				}
			}
		});
		
		ArrayList<ButtonItem> buttons = new ArrayList<ButtonItem>() ;
		buttons.add(navigateButtonItem) ;
		buttons.add(deleteButtonItem) ;
		
		return new ODIE_ButtonItemPanel("Actions", buttons) ;

	}
	
	



}
